package com.zwz.service.baseConfig;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zwz.bean.SysUsersBean;
import com.zwz.dao.BaseDao;
import com.zwz.dao.SysUserDao;
import com.zwz.dao.SysUserRoleDao;
import com.zwz.model.SysUserRole;
import com.zwz.model.SysUsers;
import com.zwz.service.BaseService;
import com.zwz.utils.DateUtils;
import com.zwz.utils.FunctionUtils;

@Service
public class SysUserService extends BaseService<SysUsers> {
	
	@Autowired
	private SysUserDao<SysUsers> sysUserDao;
	@Autowired
	private SysUserRoleDao<SysUserRole> sysUserRoleDao;

	@Override
	public BaseDao<SysUsers> getDao() {
		return sysUserDao;
	}
	/**
	 * 新增
	 * @param sysUser
	 * @return
	 */
	public synchronized int add(SysUsers sysUser) {
		//获得userId
		//格式:年份+最大userId+1去掉年份
		long maxUserId=getMaxUserId()+1;
		String currentYear=DateUtils.getCurrentYear();
		sysUser.setUser_id(currentYear+String.valueOf(maxUserId).substring(4));
		sysUser.setId(FunctionUtils.getUUID());
		sysUserDao.add(sysUser);
		SysUserRole sysUserRole=new SysUserRole();
		sysUserRole.setId(FunctionUtils.getUUID());
		sysUserRole.setUser_id(sysUser.getUser_id());
		sysUserRole.setRole_id(sysUser.getRoles_id());
		return sysUserRoleDao.add(sysUserRole);
	}
	
	/**
	 * 修改
	 */
	public int update(SysUsers sysUser){
		sysUserDao.update(sysUser);
		SysUserRole sysUserRole=new SysUserRole();
		sysUserRole.setId(sysUser.getId());
		sysUserRole.setUser_id(sysUser.getUser_id());
		sysUserRole.setRole_id(sysUser.getRoles_id());
		return sysUserRoleDao.update(sysUserRole);
	}
	
	/**
	 * 删除
	 * @param id 用户ID
	 * @return
	 */
	public int delete(String id){
		SysUsers user=new SysUsers();
		user.setUser_id(id);
		user.setStatus("-1");
		sysUserDao.updateBySelective(user);
		return sysUserRoleDao.delete(id);
	}
	
	/**
	 * 查询最大userId
	 * @return
	 */
	public long getMaxUserId(){
		return sysUserDao.getMaxUserId();
	}
	
	/**
	 * 修改用户状态
	 * @return
	 */
	public int updateUserStatus(SysUsers users){
		return sysUserDao.updateUserStatus(users);
	}
	
	/**
	 * 根据帐号密码查询(登录)
	 * @param users
	 * @return
	 */
	public Map<String, String> getUserByAccountPwd(SysUsersBean users){
		return sysUserDao.getUserByAccountPwd(users);
	}
}
